using System;
using System.Net;
using System.Windows;
using MvvmTreeView;
#if SILVERLIGHT
using AbstraX.ClientInterfaces;
#else
using AbstraX.ServerInterfaces;
#endif
using EntityProvider.Web;
#if SILVERLIGHT
using System.ServiceModel.DomainServices.Client;

namespace EntityProvider
{
    public class EntityProviderClientService
    {

        private EntityProviderContext context;

        public EntityProviderClientService()
        {
            context = new EntityProviderContext(); 
        }


        #region Service Methods

        #region EntityContainer
        public InvokeOperation<string> AddNewEntityContainer(IBase parentObject, string nodeText)
        {
            // mike todo - make service method match to above

            var invokeOperation = context.AddNewEntityContainer(parentObject.ID, nodeText);

            return invokeOperation;
        }

        public InvokeOperation<string> RenameEntityContainer(IBase parentObject, string nodeText)
        {
            var invokeOperation = context.RenameEntityContainer(parentObject.ID, nodeText);

            return invokeOperation;
        }

        public InvokeOperation<string> DeleteEntityContainer(IBase parentObject)
        {
            var invokeOperation = context.DeleteEntityContainer(parentObject.ID);

            return invokeOperation;
        }

        #endregion

        #region EntitySet
        public InvokeOperation<string> AddNewEntitySet(IBase parentObject, string nodeText)
        {
            var invokeOperation = context.AddNewEntitySet(parentObject.ID, nodeText);

            return invokeOperation;
        }

        public InvokeOperation RenameEntitySet(IBase parentObject, string nodeText, string oldName)
        {
            var invokeOperation = context.RenameEntitySet(parentObject.ParentID, nodeText, oldName);

            return invokeOperation;
        }

        public InvokeOperation DeleteEntitySet(IBase parentObject, string nodeText)
        {
            var invokeOperation = context.DeleteEntitySet(parentObject.ParentID, nodeText);

            return invokeOperation;
        }

        #endregion

        #region EntityType

        public InvokeOperation<string> AddNewEntityType(IBase parentObject, string nodeText)
        {
            var invokeOperation = context.AddNewEntityType(parentObject.ID, nodeText);

            return invokeOperation;
        }

        public InvokeOperation<string[]> RenameEntityType(IBase parentObject, string nodeText, string oldName)
        {
            var invokeOperation = context.RenameEntityType(parentObject.ID, parentObject.Parent.ParentID, nodeText, oldName);

            return invokeOperation;
        }

        public InvokeOperation DeleteEntityType(IBase parentObject, string nodeText)
        {
            var invokeOperation = context.DeleteEntityType(parentObject.ParentID, nodeText);

            return invokeOperation;
        }

        public InvokeOperation<string> AddNewRelatedEntityType(IBase parentObject)
        {
            var invokeOperation = context.AddNewRelatedEntityType(parentObject.ID);

            return invokeOperation;
        }

        public InvokeOperation<string> AddExistingRelatedEntityType(IBase parentObject)
        {
            var invokeOperation = context.AddExistingRelatedEntityType(parentObject.ID);

            return invokeOperation;
        }

        public InvokeOperation<string> RemoveRelatedEntityType(IBase parentObject)
        {
            var invokeOperation = context.RemoveRelatedEntityType(parentObject.ID);

            return invokeOperation;
        }

        #endregion

        #region Property

        public InvokeOperation<string> AddNewProperty(IBase parentObject, string nodeText)
        {
            var invokeOperation = context.AddNewProperty(parentObject.ID, nodeText);

            return invokeOperation;
        }

        public InvokeOperation<string> RenameProperty(IBase parentObject, string nodeText, string oldName)
        {
            var invokeOperation = context.RenameProperty(parentObject.ParentID, nodeText, oldName);

            return invokeOperation;
        }

        public InvokeOperation DeleteProperty(IBase parentObject, string nodeText)
        {
            var invokeOperation = context.DeleteProperty(parentObject.ParentID, nodeText);

            return invokeOperation;
        }

        public InvokeOperation<string> ExpandProperty(IBase parentObject, string dataType, string allowNull, string defaultValue, string dataSize, string keyStatus)
        {
            var invokeOperation = context.ExpandProperty(parentObject.ID, dataType, allowNull, defaultValue, dataSize, keyStatus);

            return invokeOperation;
        }

        #endregion

        #region NavigationProperty

   /*     public InvokeOperation<string> AddNewNavigationProperty(IBase parentObject, string nodeText)
        {
            var invokeOperation = context.AddNewNavigationProperty(parentObject.ID, nodeText,  nodeText,  navText);

            //treeNode.AfterLabelEdit += (sender2, e2) =>
            //{
            //    var invokeOperation = clientService.RenameNavigationProperty(baseObject, e2.Label);

        }
        */
        public InvokeOperation<string> RenameNavigationProperty(IBase parentObject, string nodeText)
        {
            var invokeOperation = context.RenameNavigationProperty(parentObject.ID, nodeText);

            //        if (invokeOperation.HasError)
            //        {
            //            throw invokeOperation.Error;
            //        }
            //        else
            //        {
            //            treeNode.Parent.LoadChildren();
            //        }
            //    };
            //};

            return null;
        }

        public InvokeOperation<string> DeleteNavigationProperty(IBase parentObject)
        {
            var invokeOperation = context.DeleteNavigationProperty(parentObject.ID);

            return invokeOperation;
        }

 /*       public InvokeOperation<string> SelectMultiplicity(IBase parentObject)
        {
            var invokeOperation = context.SelectMultiplicity(parentObject.ID);

            //    return invokeOperation;
            //}
        }*/
#endregion

#region Comments

        public InvokeOperation<string[]> ViewComments(IBase parentObject)
        {

            var invokeOperation = context.ViewComments(parentObject.ID, parentObject.GetType().Name);

            return invokeOperation;

        } 

        public InvokeOperation AddComments(IBase parentObject,string documentationSummary, string documentation)
        {
            var invokeOperation = context.AddComments(parentObject.GetType().Name, parentObject.ID, documentationSummary, documentation);

            return invokeOperation;
        }

        public InvokeOperation<string[]> EditComments(IBase parentObject)
        {
            var invokeOperation = context.EditComments(parentObject.ID, parentObject.GetType().Name);

            return invokeOperation;
        }

        public InvokeOperation checkForCommentstoDelete(IBase parentObject)
        {
            var invokeOperation = context.checkForCommentstoDelete(parentObject.ID, parentObject.GetType().Name);

            return invokeOperation;
        }

      //public InvokeOperation<bool> DeleteComments(IBase parentObject)
      //  {
      //      var invokeOperation = context.DeleteComments(parentObject.ID);

      //      return invokeOperation;
      //  }

        
#endregion

        #endregion
    }
}
#endif
